package com.aaa.controller.back;

import com.aaa.entity.*;
import com.aaa.service.back.EmpService;
import com.aaa.service.back.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping("/warehouse")
@Controller
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private EmpService empService;

    /**
     * 仓库信息显示
     * @return
     */
    @RequestMapping("/warehouseShow")
    public String warehouseShow(Model model){
        List<Map<String, Object>> warehouseList = warehouseService.findWarehouse();
        model.addAttribute("warehouseList",warehouseList);
        return "back/warehouse-list";
    }

    /**
     * 添加仓库显示
     * @return
     */
    @RequestMapping("/addWarehouseShow")
    public String addWarehouseShow(Model model){
        List<Map<String, Object>> empList = empService.findEmp();
        model.addAttribute("empList",empList);
        return "back/warehouse-add";
    }

    /**
     * 添加仓库
     * @return
     */
    @RequestMapping("/addWarehouseSubmit")
    public String addWarehouseSubmit(Warehouse warehouse){
        warehouseService.addWarehouse(warehouse);
        return "back/warehouse-add";
    }

    /**
     * 修改仓库显示
     * @return
     */
    @RequestMapping("/editWarehouseShow")
    public String editWarehouseShow(Model model,Integer warehouseId){
        List<Map<String, Object>> empList = empService.findEmp();
        model.addAttribute("empList",empList);
        Map<String, Object> warehouseMap = warehouseService.findWarehouseByWarehouseId(warehouseId);
        model.addAttribute("warehouseMap",warehouseMap);
        return "back/warehouse-edit";
    }

    /**
     * 修改仓库
     * @return
     */
    @RequestMapping("/editWarehouseSubmit")
    public String editWarehouseSubmit(Warehouse warehouse){
        warehouseService.editWarehouse(warehouse);
        return "redirect:/warehouse/editWarehouseShow?warehouseId="+warehouse.getWarehouseId();
    }

    /**
     * 查询仓库库存
     * @param model
     * @return
     */
    @RequestMapping("/warehouseDetailsShow")
    public String warehouseDetailsShow(Model model, WarehouseDetailsQuery warehouseDetailsQuery,Integer curPage,Integer pageSize){
        List<Map<String, Object>> warehouseList = warehouseService.findWarehouse();
        model.addAttribute("warehouseList",warehouseList);

        System.out.println("warehouseDetailsQuery:"+warehouseDetailsQuery);
        curPage = curPage==null?1:curPage;
        pageSize = pageSize==null?100:pageSize;

        PagerData<WarehouseDetails> pd = warehouseService.listByPage(warehouseDetailsQuery, curPage, pageSize);

        model.addAttribute("pd", pd);

        return "back/warehouseDetails-list";
    }

}
